using DimensionsHelper.Common.Utils;
using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisFunctionDeclaration : AnalysisStatementWithDomain
{
    private readonly AnalysisBlock _body;

    internal AnalysisFunctionDeclaration(
        FunctionDeclaration functionDeclaration,
        FunctionSymbol functionSymbol,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.Function, functionDeclaration, functionSymbol, parent)
    {
        Syntax = functionDeclaration;
        // Parameters
        functionDeclaration.Parameters.ForEach(p =>
        {
            AnalysisParameter parameter = new(p, this);
            Children.Add(parameter);
        });
        _body = AnalysisNodeFactory.Block(functionDeclaration.Body, this, null);
        Children.Add(_body);
    }

    public override FunctionDeclaration Syntax { get; }

    public override AnalysisBlock Body => _body;
}


public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个函数声明
    /// </summary>
    /// <param name="functionDeclaration">函数声明语法节点</param>
    /// <param name="functionSymbol">函数符号</param>
    /// <param name="parent">父节点，可以忽略</param>
    public static AnalysisFunctionDeclaration FunctionDeclaration(FunctionDeclaration functionDeclaration,
        FunctionSymbol functionSymbol, AnalysisNode? parent)
    {
        return new AnalysisFunctionDeclaration(functionDeclaration, functionSymbol, parent);
    }
}